package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.bean.Order;
import org.example.bean.OrderItem;
import org.example.mapper.OrderMapper;
import org.example.service.OrderItemService;
import org.example.service.OrderService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderItem orderItem;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private OrderItemService orderItemService;

    @Override
    public JsonResult saveOrder(Order order, String[] ids) {
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        //生成订单
        save(order);
        //生成订单详情
        //order_i book_id number
        for (int i = 0; i < ids.length; i++) {
            OrderItem item = new OrderItem();
            item.setOrderId(order.getId());
            item.setBookId(ids[i]);
            Object number = ops.get("gwc" + order.getUserId(), ids[i]);
            if (number == null) {
                item.setNumber("1");
            }else {
                item.setNumber(number.toString());
            }
            orderItemService.save(item);
        }
        return ResultTool.success("success");
    }
}
